package com.svo.weitu.model.push;

import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import android.content.Context;
import android.content.SharedPreferences;
import cn.jpush.android.api.JPushInterface;

import com.svo.weitu.util.MyUtil;

public class MPush {
	private SharedPreferences preferences;
	private Context context;
	public MPush(Context context){
		this.context = context;
		preferences = context.getSharedPreferences("push", 0);
	}
	public void init() {
		String deviceId = MyUtil.getDevId(context);
		preferences.edit().putString(deviceId, deviceId).commit();
		setTags();
//		Log.i("weitu", "devId:"+deviceId);
	}
	public void addTag(String tagName) {
		if (preferences.contains(tagName)) {
			return;
		}
		preferences.edit().putString(tagName, tagName).commit();
		setTags();
	}
	public void delTag(String tagName) {
		if (!preferences.contains(tagName)) {
			return;
		}
		preferences.edit().remove(tagName).commit();
		setTags();
	}
	private void setTags() {
		@SuppressWarnings("unchecked")
		Map<String, String> map = (Map<String, String>) preferences.getAll();
		Set<Entry<String, String>> set = map.entrySet();
		LinkedHashSet<String> sets = new LinkedHashSet<String>();
		for (Entry<String, String> entry : set) {
			sets.add(entry.getKey());
		}
		JPushInterface.setAliasAndTags(context, MyUtil.getDevId(context), sets);
	}
}
